'use strict'

import axios from 'axios'
import { Message } from 'element-ui'
import api from '../utils/api'
axios.interceptors.request.use(config => {
    // loading
    return config
}, error => {
    return Promise.reject(error)
})

axios.interceptors.response.use(response => {
    return response
}, error => {
    return Promise.resolve(error.response)
})
const isEmpty = function (msg) {
    if (msg == null || msg == 'null' || msg == 'undefined' || msg == '') {
        return true;
    }
    return false;
};

const msgError = function (_this, msg) {
    return _this.$message({
        message: msg,
        type: "error",
        showClose: true,
        duration: 2000
    });
};
const msgSuccess = function (_this, msg) {
    return _this.$message({
        message: msg,
        type: "success",
        showClose: true,
        duration: 2000
    });
};
const msgWarning = function (_this, msg) {
    return _this.$message({
        message: msg,
        type: "warning",
        showClose: true,
        duration: 2000
    });
};
function checkStatus(response) {
    // 如果http状态码正常，则直接返回数据
    if (isEmpty(response)) {
        Message({
            message: '后台错误!',
            type: 'error',
        });
        return;
    }
    if (response && (response.status === 200 && response.data.result == 'unlogin')) {
        Message({
            message: response.data.msg,
            type: 'error',
        });
        window.location.href = "/login";
    }
    if (response && (response.status === 200 && response.data.result == 'noauth')) {
        Message({
            message: response.data.msg,
            type: 'error',
        });
        return;
    }
    if (response && (response.status === 200 || response.status === 304)) {
        return response
        // 如果不需要除了data之外的数据，可以直接 return response.data
    } else if (response && (response.status === 401)) {
        Message({
            message: '您没有权限!',
            type: 'error',
        });
        return response;
    } else if (response && (response.status === 400)) {
        Message({
            message: '请求参数错误!',
            type: 'error',
        });
    } else {
        console.log('获取不到状态吗')
    }
}
function checkCode(res) {
    if (res.status === -404) {
        Message({
            message: res.msg,
            type: 'error',
        });
    }
    if (res.data && (!res.data.success)) {
        Message({
            message: res.data.msg,
            type: 'error',
        });
    }
    return res
};
export {
    mapArr,
    isEmpty,
    msgError,
    msgSuccess,
    msgWarning,
}
export default {
    post(url, params) {
        return axios({
            method: 'post',
            //baseURL: "http://192.168.0.176:8080",
            baseURL: api.baseUrl_online,
            // baseURL: api.baseUrl_local,
            url,
            withCredentials: true,
            data: params,
            timeout: 120000,
            headers: {
                Accept: "application/json",
                "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
            }
        }).then(
            (response) => {
                return checkStatus(response)
            })
            .catch(
                (res) => {
                    return checkCode(res)
                }
            )
    },
    postFile(url, params) {
        return axios({
            method: 'post',
            baseURL: api.baseUrl_online,
            url,
            data: params,
            timeout: 120000,
            headers: {
                Accept: "application/json",
                "Content-Type": "application/x-www-form-urlencoded;charset=UTF-8"
            }
        }).then(
            (response) => {
                return checkStatus(response)
            })
            .catch(
                (res) => {
                    return checkCode(res)
                }
            )
    },
    downloadFile(url, name) {//下载
        if (!url) {
            return
        }
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.onload = function () {
            document.body.removeChild(link);
        };
        link.setAttribute('download', name);
        // link.target = 'self';
        document.body.appendChild(link);
        link.click();
    },
    downloadPhone(myUrl, name) {
        if (!myUrl) {
            return
        }
        let url = window.URL.createObjectURL(new Blob([myUrl]));
        let link = document.createElement('a');
        link.style.display = 'none';
        link.href = url;
        link.setAttribute('download', name);
        document.body.appendChild(link);
        link.click();
    }
}